package com.lone.smallmovie.controller;

import com.lone.smallmovie.model.SmsCode;
import com.lone.smallmovie.model.User;
import com.lone.smallmovie.service.impl.SmsCodeServiceImpl;
import com.lone.smallmovie.service.impl.UserServiceImpl;
import com.lone.smallmovie.utils.MapResultUtils;
import com.lone.smallmovie.utils.SmsCodeMaping;
import com.lone.smallmovie.utils.SmsCodeUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

@Controller
@RequestMapping("/smsCode")
public class SmsCodeController {

    private Logger log = Logger.getLogger(SmsCodeController.class);

    @Autowired
    private SmsCodeServiceImpl smsCodeService;
    @Autowired
    private UserServiceImpl userService;


    @RequestMapping(value = "/sendSmsCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> sendSmsCode(HttpServletRequest request, Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        String mobileNumber = request.getParameter("mobileNumber");
        String type = request.getParameter("type");
        if (mobileNumber == null || mobileNumber == "" || type == null || type == "") {
            return MapResultUtils.parameterError();
        } else {
            User user = userService.queryUserByUserAccount(mobileNumber);

            if (type.equals("1")) {//type:1  注册时发送验证码(该手机号码还未注册)
                if (user != null) {
                    map.put("status", "2");
                    map.put("message", "该号码已被注册！");
                } else {
                    sendSmsCode(mobileNumber, map);
                }
            } else if (type.equals("2")) {//type:2  该手机号应该已经注册
                if (user == null) {
                    map.put("status", "5");
                    map.put("message", "该手机号码还未注册！");
                } else {
                    sendSmsCode(mobileNumber, map);
                }

            }
        }


        return map;
    }


    //发送验证码并返回
    private void sendSmsCode(String mobileNumber, Map<String, Object> map) {
        String smsCode = SmsCodeMaping.instance().getSmsCode();
        log.info("用户 " + mobileNumber + "获取验证码----- " + smsCode);
        boolean isSend = SmsCodeUtils.sendSmsCode(mobileNumber, smsCode);
        if (isSend) {
            int result = smsCodeService.sendSmsCode(mobileNumber, smsCode);
            log.info("短信验证码是   " + result);
            if (result == 1) {
                map.put("status", "0");
                map.put("message", "短信验证码已发送，请注意查收！");
                smsCodeTimerTask(mobileNumber, smsCode);
            } else {
                map.put("status", "4");
                map.put("message", "发送短信验证码异常！！");
            }
        } else {
            map.put("status", "3");
            map.put("message", "验证码发送失败！");
        }
    }

    @RequestMapping(value = "/checkSmsCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> checkSmsCode(HttpServletRequest request, Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        String mSmsCode = request.getParameter("smsCode");
        String mPhoneNumber = request.getParameter("mobileNumber");
        if (mSmsCode == null || mSmsCode == "" || mPhoneNumber == null || mPhoneNumber == "") {
            return MapResultUtils.parameterError();
        }
        log.info("用户 "+mPhoneNumber+"验证验证码----- "+mSmsCode);
        SmsCode smsCode = smsCodeService.querySmsCodeByMobileNumber(mPhoneNumber, mSmsCode);
        if (smsCode == null) {
            map.put("status", "1");
            map.put("message", "短信验证码无效！");
        } else {
            map.put("status", "0");
            map.put("message", "短信验证码正确！");
        }

        return map;
    }

    private void smsCodeTimerTask(final String phoneNumber, final String smsCode) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                int result = smsCodeService.updateSmsCodeStatus(phoneNumber, smsCode);
                log.info("把超过5分钟的验证码变为无效：" + result);
            }
        }, 60000 * 5);// 设定指定的时间time,此处为60000毫秒(1分钟)
    }
}
